<?php
/**
 * 上传单图
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

class Video extends Field
{
    public static $html = <<<EOF
    <div class="layui-form-item">
    <label for="[FIELD_NAME]" class="layui-form-label">[FORM_NAME]</label>
    <div class="layui-input-inline">
        <div class="layui-btn" id="[FIELD_NAME]"> 上传[FORM_NAME]</div>
        <div class="layui_img">
            <div class="layui-progress video_progress " id="[FIELD_NAME]_progress" lay-showPercent="yes"
                lay-filter="progressBar[FIELD_NAME]">
                <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
            </div>
            <video src="{\$data.[FIELD_NAME]|default=''}" controls {if !isset(\$data.[FIELD_NAME]) ||\$data.[FIELD_NAME]=='' } style="display: none;" {/if} id="form[FIELD_NAME]"
                class="formvideo"></video>
            <input type="hidden" name="[FIELD_NAME]" id="input[FIELD_NAME]"
                value="{\$data.[FIELD_NAME]|default=''}">
        </div>
    </div>
    <script>
        layui.use('upload', function () {
            var upload = layui.upload;
            upload.render({
                elem: '#[FIELD_NAME]'
                , url: "{:url('upfile/upload')}"
                , acceptMime: 'video/*'
                , accept: 'video'
                , size: 10240
                , progress: function (n, elem, res, index) {
                    $('#[FIELD_NAME]_progress').show();
                    var percent = n + '%'
                    element.progress('progressBar[FIELD_NAME]', percent);
                }
                , done: function (res, index, upload) {
                    if (res.code == 1) {
                        $("#input[FIELD_NAME]").val(res.data.url);
                        $('#[FIELD_NAME]_progress').hide();
                        $("#form[FIELD_NAME]").attr('src', res.data.url).show();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 2000 });
                        element.progress('progressBar[FIELD_NAME]', '0%');
                        $('#[FIELD_NAME]_progress').hide();
                    }


                }
            });
        })
    </script>
</div>\n
EOF;

    public static $rules = [
        'required'   => '非空',
        'file_size'  => '文件大小限制',
        'file_video' => '视频类型',
        'regular'    => '自定义正则'
    ];


    //控制器添加上传
    public static $controllerAddCode =
        <<<EOF
            //处理[FORM_NAME]上传
            \$attachment_[FIELD_NAME] = new \app\common\model\Attachment;
            \$file_[FIELD_NAME]       = \$attachment_[FIELD_NAME]->upload('[FIELD_NAME]');
            if (\$file_[FIELD_NAME]) {
                \$param['[FIELD_NAME]'] = \$file_[FIELD_NAME]->url;
            } else {
                return error(\$attachment_[FIELD_NAME]->getError());
            }
            \n
EOF;


    //控制器修改上传
    public static $controllerEditCode =
        <<<EOF
            //处理[FORM_NAME]上传
            if (!empty(\$_FILES['[FIELD_NAME]']['name'])) {
                \$attachment_[FIELD_NAME] = new \app\common\model\Attachment;
                \$file_[FIELD_NAME]       = \$attachment_[FIELD_NAME]->upload('[FIELD_NAME]');
                if (\$file_[FIELD_NAME]) {
                    \$param['[FIELD_NAME]'] = \$file_[FIELD_NAME]->url;
                }
            }
            \n
EOF;


    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace(array('[FORM_NAME]', '[FIELD_NAME]'), array($data['form_name'], $data['field_name']), $html);
        return $html;
    }
}